package ${bussPackage}.controller.${entityPackage};

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


import com.base.web.BaseAction;
import com.base.util.HtmlUtil;
import com.base.entity.BaseEntity.DELETED;
import ${bussPackage}.entity.${entityPackage}.${className};
import ${bussPackage}.page.${entityPackage}.${className}Page;
import ${bussPackage}.service.${entityPackage}.${className}Service;


/**
 * ${tableComment}
 * <pre>
 * @className : ${className}Controller
 * @tableName : ${tableName}
  * @author : zhangxiaojin
 * </pre>
 */
@Controller
@RequestMapping(value = "/${classname}")
public class ${className}Controller extends BaseAction{

	private final static Logger log= Logger.getLogger(${className}Controller.class);
	
	@Autowired
	private ${className}Service<${className}>  ${classname}Service;
	
	
	/**
	 * 首页
	 */
	@RequestMapping(value = "/indexUI",method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView indexUI(${className}Page page,HttpServletRequest request)  throws Exception{  
		Map<String,Object>  context = getRootMap();
		return forword("/${bussPackage}/${entityPackage}/${classname}",context); 
	}
	
	/**
	 * 查询列表
	 */
	@RequestMapping("/dataList") 
	public void  datalist(${className}Page page,HttpServletResponse response) throws Exception{
		List<${className}> dataList = ${classname}Service.queryByList(page);
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("total",page.getPager().getRowCount());
		jsonMap.put("rows", dataList);
		HtmlUtil.writerJson(response, jsonMap);
	}
	

	/**
	 * 保存数据（添加、修改）
	 */
	@RequestMapping(value = "/save",method = {RequestMethod.POST })
	public void save(${className} entity,HttpServletRequest request, HttpServletResponse response) throws Exception{ 
		Map<String,Object>  context = new HashMap<String,Object>();
<#if pk.columnDataType=='int' >
		if(entity.getId()==0){
<#elseif pk.columnDataType=='bigint' >
		if(entity.getId()==null){
<#elseif pk.columnDataType=='varchar' >
		if(entity.getId()==null){
</#if>
			${classname}Service.add(entity);
		}else{
			${classname}Service.update(entity);
		}
		sendSuccessMessage(response, "保存成功~");
	}
	
	/**
	 * 获取对象
	 */
	@RequestMapping("/getId")
	public void getId(String id,HttpServletResponse response) throws Exception{
		Map<String,Object>  context = new HashMap();
		${className} entity  = ${classname}Service.queryById(id);
		if(entity  == null){
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", entity);
		HtmlUtil.writerJson(response, context);
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public void delete(String[] id,HttpServletResponse response) throws Exception{
		${classname}Service.delete(id);
		sendSuccessMessage(response, "删除成功");
	}
	

}